/**
 * 
 */
package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

/**
 * @author Aslanbek
 *
 */
public class MBPMValidationSet extends X_BPM_ValidationSet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3247564880534926751L;

	/**
	 * @param ctx
	 * @param BPM_ValidationSet_ID
	 * @param trxName
	 */
	public MBPMValidationSet(Properties ctx, int BPM_ValidationSet_ID,
			String trxName) {
		super(ctx, BPM_ValidationSet_ID, trxName);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param ctx
	 * @param rs
	 * @param trxName
	 */
	public MBPMValidationSet(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
		// TODO Auto-generated constructor stub
	}

	public static MBPMValidationSet[] getMbpmValidationSets(int BPM_VersionBudget_ID){
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		MBPMValidationSet result = null;
		ArrayList<MBPMValidationSet> list = new ArrayList<MBPMValidationSet>();
		
		String sql_ = "SELECT "+X_BPM_ValidationSet.COLUMNNAME_BPM_ValidationSet_ID+" FROM "+X_BPM_ValidationSet.Table_Name
																			+" WHERE BPM_VersionBudget_ID="+BPM_VersionBudget_ID;
		try
		{
			pstmt = DB.prepareStatement(sql_,null);
			rs = pstmt.executeQuery();	
			while(rs.next()){
				result = new MBPMValidationSet(Env.getCtx(), rs.getInt(1), null);
				list.add(result);
			}				
		}
		catch (SQLException e)
		{
			CLogger.get().log(Level.INFO, "MBPMValidationSet", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}	
		
		return list.toArray(new MBPMValidationSet[list.size()]);
	}
	
	/** */
	private MBPMValidationSetLine[] m_lines = null;
	
	public MBPMValidationSetLine[] getLines(boolean requery){
		if (m_lines != null && !requery) {
			set_TrxName(m_lines, get_TrxName());
			return m_lines;
		}
		List<MBSCNetWorkDiagLine> list = new Query(getCtx(), I_BPM_ValidationSetLine.Table_Name, "BPM_ValidationSet_ID = ?", get_TrxName())
										.setParameters(get_ID())
										.setOrderBy(I_BPM_ValidationSetLine.COLUMNNAME_BPM_ValidationSetLine_ID)
										.list();
		m_lines = list.toArray(new MBPMValidationSetLine[list.size()]);
		return m_lines;
	}
}
